package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.utils.LogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/4/10 0010.
 */

public class RefundOrderAdapter extends RecyclerView.Adapter<RefundOrderAdapter.OrderHolder> {
    private List<Orders.OrdersBean.OrderBean> mOrderList;
    private Context mContext;

    public RefundOrderAdapter(List<Orders.OrdersBean.OrderBean> orderList) {
        this.mOrderList = orderList;
    }

    class OrderHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_total_count)
        TextView tvTotalCount;
        @BindView(R.id.tv_total_price)
        TextView tvTotalPrice;
        @BindView(R.id.tv_total_price_at_last)
        TextView tvTotalPriceAtLast;
        @BindView(R.id.rv_product)
        RecyclerView rvProduct;

        public OrderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick({R.id.btn_see_details})
        void onItemViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_see_details:
                    mSeeDetailListener.onSeeDetailClick(getAdapterPosition());
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    public OrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_refund, parent, false);
        OrderHolder holder = new OrderHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(OrderHolder holder, int position) {
        Orders.OrdersBean.OrderBean order = mOrderList.get(position);
        ProductAdapter productAdapter = new ProductAdapter(order.getProducts());
        int totalCount = order.getProductCount();
        double totalPrice = order.getProductAmountTotal();
        String orderCarriage = order.getOrderCarriage();
        // TODO: 2018/4/19 0019 运费
        if (orderCarriage == null) {
            orderCarriage = "0.0";
        }
        LogUtil.d("totalCount : " + totalCount + "  totalPrice : " + totalPrice + "orderCarriage : " + orderCarriage);
        holder.tvTotalCount.setText("共" + String.valueOf(totalCount) + "件商品" + "  ");
        holder.tvTotalPrice.setText("合计￥" + String.valueOf(totalPrice) + "(含运费￥");
        holder.tvTotalPriceAtLast.setText(orderCarriage + ")");
        holder.rvProduct.setAdapter(productAdapter);
        holder.rvProduct.setLayoutManager(new LinearLayoutManager(mContext));
    }

    @Override
    public int getItemCount() {
        return mOrderList.size();
    }

    public void setData(List<Orders.OrdersBean.OrderBean> list) {
        mOrderList.clear();
        mOrderList.addAll(list);
        notifyDataSetChanged();
    }

    private SeeDetailListener mSeeDetailListener;

    public interface SeeDetailListener {
        void onSeeDetailClick(int position);
    }

    public void setSeeDetailListener(SeeDetailListener listener) {
        this.mSeeDetailListener = listener;
    }

    public void clearData() {
        mOrderList.clear();
        notifyDataSetChanged();
    }

}
